package br.com.iswe.gwt.crud.client;

import java.util.List;

import br.com.iswe.core.entidades.gwt.BeanMap;

import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;

public abstract class IconGridPanel extends BeanMapListPanel {

	public IconGridPanel(){}

	public void setupScreen(){
		getFlexTable().setStyleName("iswe-iconGridPanel");
	}
	
	protected int columns = 4;//default value
	
	@Override
	protected void buildRow(int row, BeanMap item) {
		//Do nothing
		//Think about create another class which IconGrid and List classes would be based and move the common characteristics to this new class.
	}
	
	public void populate(List<BeanMap> queryResult) {
		//clean the felxTable
		while(getFlexTable().getRowCount() > 0){
			getFlexTable().removeRow(0);
		}

		checkBoxList.clear();
		
		int row = 0;
		int col = 0;
		for (int i = 0; i < queryResult.size(); i++) {
			final BeanMap item = queryResult.get(i);
			
			buildIcon(row, col++, item);
			
			if(col >= columns){
				col = 0;
				row++;
			}
			
			if((row % 2) == 0){
				getFlexTable().getRowFormatter().setStyleName(row, "iswe-listRowPar");
			}
			else{
				getFlexTable().getRowFormatter().setStyleName(row, "iswe-listRowImpar");
			}
		}
		
		if(hasPagination()){
			int cols = getFlexTable().getCellCount(row);
			
			row = getFlexTable().getRowCount();
			PaginationPanel paginationPanel = getPaginationPanel();
			getFlexTable().setWidget(row, 0, paginationPanel);
			getFlexTable().getFlexCellFormatter().setColSpan(row, 0, cols);
			getFlexTable().getFlexCellFormatter().setAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		}	
	}
	
	protected abstract void buildIcon(int row, int col, BeanMap item);

	public int getColumns() {
		return columns;
	}
	public void setColumns(int columns) {
		this.columns = columns;
	}
}
